#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn = 10005;
int n,m,num_edge;
int fa[maxn],ans;
int num[4];

struct node{
    int from;
    int next;
    int dis;
    bool operator < (const node &b)const{
        return dis<b.dis;
    }
}edge[maxn];

void add_edge(int from,int next,int dis){
    num_edge++;
    edge[num_edge].from=from;
    edge[num_edge].next=next;
    edge[num_edge].dis=dis;
}

void read(string s){
	int cnt=1,tmp=0;
	char c;
	for(int i=1;i<s.size();i++){
		c=s[i];
		if(c==';'){
			add_edge(num[1],num[2],tmp);
			cnt=1;
			tmp=0;
		}else if(c==','){
			num[cnt++]=tmp;
			tmp=0;
		}else if((c-'0')>=0 && (c-'0')<=9){
			tmp=tmp*10+c-'0';
		}
	}
	add_edge(num[1],num[2],tmp);
}

int find(int a){
    return fa[a]==a?a:fa[a]=find(fa[a]);
}

bool join(int u,int v){
    int f1=find(u);int f2=find(v);
    if(f1==f2){
        return false;
    }else{
        fa[f1]=f2;
        return true;
    }
}

void kru(){
    sort(edge+1,edge+1+num_edge);
    for(int i=1;i<=n;i++)fa[i]=i;
    int cnt=0;
    for(int i=1;i<=num_edge;i++){
        int u=edge[i].from;
        int v=edge[i].next;
        if(join(u,v)){
            cnt++;
            ans+=edge[i].dis;
            if(cnt>=n)return;
        }
    }
}

int main(){
	string s;
	while(cin>>n>>s){
		ans=0;
		num_edge=0; 
		read(s);
		kru();
		cout<<ans<<endl;
	}
	
	
	return 0;
}

